/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frame.BanHang;

import gui.customcbb;
import java.awt.HeadlessException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.Nhanvien;

/**
 *
 * @author Cong
 */
public class QLDoanhThu extends myclass.MyAdminFrame {

    public static String TENKHUVUC = "Quản Lý Doanh Thu";
    private DefaultComboBoxModel cboxModelcuahang;
    private DefaultComboBoxModel cboxModelloai;
    private DefaultTableModel tbModeldoanhthu;
    private static final String[] columns = {"Loại", "Tổng doanh thu", "Ngày", "Cửa hàng"};

    public QLDoanhThu(Nhanvien nv) {
        super(nv);
        initComponents();
        cboxModelcuahang = new DefaultComboBoxModel();
        cboxModelloai = new DefaultComboBoxModel();
        tbModeldoanhthu = new DefaultTableModel();
        tbModeldoanhthu.setColumnIdentifiers(columns);
        this.jcbbcuahang.setModel(cboxModelcuahang);
        this.jcbbloai.setModel(cboxModelloai);
        this.jtbdoanhthu.setModel(tbModeldoanhthu);
        LoadCBB();
        LoadTB();
    }

    private void LoadCBB() {
        List ds = dao.daoDoanhThu.laydsCuaHang();
        if (ds != null) {
            cboxModelcuahang.removeAllElements();
            cboxModelloai.removeAllElements();
            for (int i = 0; i < ds.size(); i++) {
                pojo.Cuahang ch = (pojo.Cuahang) ds.get(i);
                cboxModelcuahang.addElement(ch);
            }
        }
        cboxModelloai.addElement(new customcbb(1, "Ngày"));
        cboxModelloai.addElement(new customcbb(2, "Tháng"));
        cboxModelloai.addElement(new customcbb(3, "Quý"));
        cboxModelloai.addElement(new customcbb(4, "Năm"));
    }

    private void LoadTB() {
        List ds = dao.daoDoanhThu.laydsDoanhThulimit();
        SimpleDateFormat fm = new SimpleDateFormat("dd/MM/yyyy");
        double tdt = 0;
        if (ds != null) {
            ClearData();
            for (int i = 0; i < ds.size(); i++) {
                pojo.Doanhthu dt = (pojo.Doanhthu) ds.get(i);
                tdt += dt.getTongdoanhthu();
                tbModeldoanhthu.addRow(new Object[]{getvalue(dt.getLoai()), dt.getTongdoanhthu(), fm.format(dt.getNgay()), dt.getCuahang().getTencuahang()});
            }
        }
        this.jlbtongdoanhthu.setText(tdt + " VNĐ");
    }

    private void ClearData() {
        DefaultTableModel dtm = (DefaultTableModel) jtbdoanhthu.getModel();
        dtm.getDataVector().clear();
    }

    private String getvalue(long key) {
        String value = "";
        switch ((int) key) {
            case 1:
                value = "Ngày";
                break;
            case 2:
                value = "Tháng";
                break;
            case 3:
                value = "Quý";
                break;
            case 4:
                value = "Năm";
                break;
        }
        return value;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jcbbloai = new javax.swing.JComboBox();
        jcbbcuahang = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jtfngay = new javax.swing.JTextField();
        jbtthongke = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbdoanhthu = new javax.swing.JTable();
        jlbtongdoanhthu = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jbtcancel = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("Thống kê doanh thu");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Thống kê doanh thu");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin thống kê"));

        jLabel2.setText("Thống kê theo:");

        jLabel3.setText("Cửa hàng:");

        jcbbloai.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jcbbcuahang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel4.setText("Ngày:");

        jbtthongke.setText("Lập thống kê");
        jbtthongke.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtthongkeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(37, 37, 37)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jcbbloai, 0, 186, Short.MAX_VALUE)
                    .addComponent(jcbbcuahang, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbtthongke)
                    .addComponent(jtfngay, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jcbbloai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jtfngay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jcbbcuahang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtthongke))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jtbdoanhthu.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtbdoanhthu);

        jlbtongdoanhthu.setForeground(new java.awt.Color(255, 0, 0));
        jlbtongdoanhthu.setText("0 VNĐ");

        jLabel6.setText("Tổng doanh thu:");

        jbtcancel.setText("Đóng");
        jbtcancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtcancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 579, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jLabel6)
                                .addGap(18, 18, 18)
                                .addComponent(jlbtongdoanhthu)))
                        .addContainerGap())
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jbtcancel)
                        .addGap(266, 266, 266))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(19, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbtongdoanhthu)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtcancel)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtthongkeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtthongkeActionPerformed
        pojo.Cuahang ch = (pojo.Cuahang) jcbbcuahang.getSelectedItem();
        customcbb loai = (customcbb) jcbbloai.getSelectedItem();
        long keyloai = loai.getKey();
        double tdt = 0;
        SimpleDateFormat fm = new SimpleDateFormat("dd/MM/yyyy");
        String st = jtfngay.getText();
        List ds = null;
        try {
            Date ngay = fm.parse(st);

            if (keyloai == 1) {
                ds = dao.daoDoanhThu.laydsDoanhThuNgay(ngay, ch);
            }
            if (keyloai == 2) {
                ds = dao.daoDoanhThu.laydsDoanhThuThang(ngay, ch);
            }
            if (keyloai == 3) {
                ds = dao.daoDoanhThu.laydsDoanhThuQuy(ngay, ch);
            }
            if (keyloai == 4) {
                ds = dao.daoDoanhThu.laydsDoanhThuNam(ngay, ch);
            }
            if (!ds.isEmpty()) {
                ClearData();
                for (int i = 0; i < ds.size(); i++) {
                    pojo.Doanhthu dt = (pojo.Doanhthu) ds.get(i);
                    tdt += dt.getTongdoanhthu();
                    tbModeldoanhthu.addRow(new Object[]{getvalue(dt.getLoai()), dt.getTongdoanhthu(), fm.format(dt.getNgay()), dt.getCuahang().getTencuahang()});
                }
                this.jlbtongdoanhthu.setText(tdt + " VNĐ");
            } else {
                JOptionPane.showMessageDialog(this, "Không tìm thấy thông tin theo yêu cầu.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            }
        } catch (ParseException | HeadlessException ex) {
            JOptionPane.showMessageDialog(this, "Ngày không hợp lệ.", "Thông báo", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jbtthongkeActionPerformed

    private void jbtcancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtcancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_jbtcancelActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtcancel;
    private javax.swing.JButton jbtthongke;
    private javax.swing.JComboBox jcbbcuahang;
    private javax.swing.JComboBox jcbbloai;
    private javax.swing.JLabel jlbtongdoanhthu;
    private javax.swing.JTable jtbdoanhthu;
    private javax.swing.JTextField jtfngay;
    // End of variables declaration//GEN-END:variables
}
